<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] - ".__(($_GET["type"]=="add"? "Add" : "Update")." Member Bank In");
$page_title=__(($_GET["type"]=="add"? "Add" : "Update")." Member Bank In");
$wit_list_file="bankin_list.php";

if(!in_array($_GET["type"], array("add","edit"))){
	header("location: $this_file?type=add");
	exit();
}

if($_GET["type"]=="edit"){
	if(!$get_s["id"]){
		$errmsg=__("No bank in with that ID found!");
	}elseif(!@mysql_num_rows($r=mysql_query("select * from $db->bank_transaction where id='$get_d[id]'"))){
		$errmsg=__("No bank in with that ID found!");
	}
}

if($errmsg){
	$errmsg="<h2>$page_title</h2>".format_err($errmsg);

	print format_admin_page($errmsg, $this_title);
	exit();
}

$r_status=array("pending","approved","rejected");
$r_status_edit=array("approved","rejected");
$r_status_add=array("pending","approved");
$r_status_d=array("pending"=>__("Pending"),"approved"=>__("Approved"),"rejected"=>__("Rejected"));$r_currency=array("HKD","IND","INR","JYP","MYR","RMB","SGD","THB","TWD","USD");$r_currency_d=array("HKD"=>"HKD","IND"=>"IND","INR"=>"INR","JYP"=>"JYP","MYR"=>"MYR","RMB"=>"RMB","SGD"=>"SGD","THB"=>"THB","TWD"=>"TWD","USD"=>"USD");
$td_width=180;
$datetime=ndate($vars["system_date_format"]);

//#####ADD / EDIT BANK IN POST#####
if($_POST["__req"]){
	//#####ERROR CHECK#####
	$errmsg=verify_form_data("bank_transaction", $post_s);
	if(!$errmsg){		if($_FILES["upload_file"]["name"]==''){			$filename = '';		}else{			if ($_FILES["upload_file"]["error"] > 0){			  echo "Error: " . $_FILES["upload_file"]["error"] . "<br>";			}else{			  $filename = date('YmdHis').'_'.$_FILES["upload_file"]["name"];			  move_uploaded_file($_FILES["upload_file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/upload/bankin/" .$filename);			  			}		}
		if($_GET["type"]=='add'){			
			//check status
			if(!in_array($post_s["status"], $r_status_add)){
				$errmsg.=__("You have selected an invalid status.")."<br />\n";
			}
		}elseif($_GET["type"]=="edit"){
			//check status
			if($r_wit["status"]=="approved" && !in_array($post_s["status"], $r_status_edit)){
				$errmsg.=__("You have selected an invalid status.")."<br />\n";
			}elseif($r_wit["status"]=="pending" && !in_array($post_s["status"], $r_status)){
				$errmsg.=__("You have selected an invalid status.")."<br />\n";
			}
		}
		if($_GET["type"]=="add" || ($_GET["type"]=="edit" && $r_wit["status"]=="pending")){
			//approval date
			if(strlen($post_s["adate"]) && !verify_date($post_s["adate"])){
				$errmsg.=__("You have provided an invalid approval date.")."<br />\n";
			}
		}
	}
	//#####END ERROR CHECK#####

	//#####ADD TO DB#####
	if(!$errmsg){
		if($_GET["type"]=="add"){//add							$sql="insert into $db->bank_transaction (filename, currency, amount, code, eswallet, status, bdate, cdate, create_by, remark1) values ('$filename', '$post_d[currency]', '$post_d[_amount]', '$post_d[_code]', '$post_d[_eswallet]', '$post_d[_bdate]', '$datetime', '$_SESSION[aname]', '$post_d[_remark1]')";
			if(!mysql_query($sql)){
				$errmsg.=__("Error creating a new bank in record.")."<br />\n<br />\n".__("SQL:")." $sql<br />\n<br />\n".__("Error:")." ".mysql_error()."<br />\n";
			}else{
				$new_wid=mysql_insert_id();
			}
			if(!$errmsg){
				$msg=__("The new bank in record has been successfully created.")."<br />\n".($ewrec_errmsg? "<br />\n<span class='red'>$ewrec_errmsg</span>" : "");
				$logstr="The new bank in record has been successfully created.<br />\n".($ewrec_errmsg? "<br />\n<span class='red'>$ewrec_errmsg</span>" : "");
				if($ewrec_errmsg){
					$critical_error=$msg;
				}
			}else{
				if($ewrec_errmsg){
					$errmsg.=$ewrec_errmsg;
				}
				$critical_error=$errmsg;
			}
		}else{//update						if($filename==''){
				$sql="update $db->bank_transaction set currency='$post_d[currency]', amount='$post_d[_amount]', code='$post_d[_code]', status='$post_d[status]', bdate='$post_d[_bdate]', create_by='$_SESSION[aname]', remark1='$post_d[_remark1]' where id='$get_d[id]' limit 1";			}else{				$sql="update $db->bank_transaction set filename='$filename', currency='$post_d[currency]', amount='$post_d[_amount]', code='$post_d[_code]', status='$post_d[status]', bdate='$post_d[_bdate]', create_by='$_SESSION[aname]', remark1='$post_d[_remark1]' where id='$get_d[id]' limit 1";			}			
			if(!mysql_query($sql)){
				$errmsg.=__("Error updating the bank in record.")."<br />\n<br />\n".__("SQL:")." $sql<br />\n<br />\n".__("Error:")." ".mysql_error()."<br />\n";
				$critical_error.="Error updating the bank in record.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
			}
			if(!$errmsg){
				$r_wit=mysql_fetch_assoc(mysql_query("select * from $db->bank_transaction where id='$get_d[id]'"));
				$msg=__("The bank in record has been successfully updated.")."<br />\n".($ew_errmsg? "<br />\n<span class='red'>$ew_errmsg</span>" : "").($ew_msg? "<br />\n$ew_msg" : "").($ewrec_errmsg? "<br />\n<span class='red'>$ewrec_errmsg</span>" : "");
				if(!$critical_error){
					$logstr="The bank in record has been successfully updated.<br />\n"; 
				}else{
					$critical_error="The bank in record has been successfully updated.<br />\nHowever, some error occur:<br /><br />$critical_error";
				}
			}elseif($ew_errmsg || $ewrec_errmsg){
				$errmsg.=$ew_errmsg.$ewrec_errmsg;
			}
		}
	}

	//log critical error
	$log_code=$_GET["type"]=="add"? "a-mwa" : "a-mwe";
	$wid = $_GET["type"]=="add"? $new_wid : $get_d['id'];
	$msg=$msg? format_msg($msg) : "";
	$errmsg=$errmsg? format_err(__("We have encountered some error(s):")."<br />\n<br />\n$errmsg") : "";
}
//#####END BANK IN POST#####
$form_fields=array("filename"=>"","_bdate"=>"","_code"=>"","currency"=>"","_amount"=>"","_remark1"=>"","status"=>"pending");
foreach($form_fields as $field => $default){
	$db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
	$dis[$field]=(!$posting? ($_GET["type"]=="edit"? $r_wit[$db_fieldname] : $default) : $post_h[$field]);
}
$status_select=($_GET["type"]=="add"? build_select($r_status_add, $r_status_d, $dis["status"], "status", $inputbox_style) : ($r_wit["status"]=="pending"? build_select($r_status, $r_status_d, $dis["status"], "status", $inputbox_style) : ($r_wit["status"]=="paid"? build_select($r_status_edit, $r_status_d, $dis["status"], "status", $inputbox_style) : $r_status_d[$r_wit["status"]])));
$currency_select=build_select($r_currency, $r_currency_d, $dis["currency"], "currency", $inputbox_style);
//javascript
$jvscript=
"<script type='text/javascript' src='".JS_URL."/get_file_gzip.php?file=".urlencode("common.js,jquery.js")."'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
 j('form[@name=wit_form]').submit(function(){
  j('input[@name=submit_btn]').attr('disabled','disabled');
 });
});
</script>";

//back button
$url_referer=$_SERVER["HTTP_REFERER"];
if($post_s["back_url"]){
	$back_url=$post_s["back_url"];
}elseif($url_referer && !strstr($url_referer, $this_file)){
	$back_url=$url_referer;
}else{
	$back_url=$wit_list_file;
}
array("filename"=>"","_bdate"=>"","_code"=>"","currency"=>"","_amount"=>"","_remark1"=>"","status"=>"pending");
$display_fields=array("filename","_bdate","_code","currency","_amount","_remark1","status");
foreach($form_fields as $field => $default){
 if(in_array($field, $display_fields)){
  $db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
  $dbr=explode("#", $vars["dbr"]["member_withdraw"][$db_fieldname]);
  $readonly=$textarea=false;
  $extra_display='';
  if(in_array($db_fieldname, array("_remark1"))){
   $textarea=true;
  }
  $readonly_str=$readonly? "readonly='readonly'" : "";
  $form_inputfield[$db_fieldname]="
  <tr>
   <td width='$td_width'>".__($dbr[4]).__(":").($dbr[3]=='m'? " ".__("*") : "")."</td>
   <td>".($textarea? "<textarea name='$field' rows='6' $inputbox_style $readonly_str>".$dis[$field]."</textarea>" : "
   <input type='text' name='$field' $inputbox_style value=\"".$dis[$field]."\" $readonly_str />")."$extra_display</td>
  </tr>";
 }
}

$manage_wit=($errmsg || $msg?
"$errmsg $msg" : "")."
<form name='wit_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]' enctype='multipart/form-data'>
<input type='hidden' name='__req' value='1' />
<input type='hidden' name='back_url' value=\"$back_url\" />
<table class='amt_table'> <tr>
  <td colspan='2' class='center' style='padding:20px 0 20px 0;'>
   <input type='button' value='".__("Back")."' onclick=\"location='$back_url';\">&nbsp;&nbsp;&nbsp;
   <input type='submit' name='submit_btn' value=\"".__(($_GET["type"]=="edit"? "Update" : "Add")." Withdrawal")."\" />
  </td>
 </tr>
 <tr class='amt_header'>
  <td colspan='2'>".__("Bank In Information")."</td>
 </tr>".($_GET["type"]=="edit"? "
 <tr>
  <td width='$td_width'>".__("Bank In ID").__(":")."</td>
  <td>$get_s[id]</td>
 </tr>" : "")." <tr>  	<td>".__("Upload File").__(":")."</td>  	<td><input name='upload_file' id='upload_file' type='file' onkeypress='return false' /></td>  	 </tr>
 $form_inputfield[bdate]
 $form_inputfield[code] <tr>  <td>".__("Currency").__(":")." ".__("*")."</td>  <td>$currency_select</td> </tr>
 $form_inputfield[amount]
 $form_inputfield[remark1]
 <tr>
  <td>".__("Status").__(":")." ".__("*")."</td>
  <td>$status_select</td>
 </tr>
 <tr>
  <td colspan='2' class='center' style='padding:20px 0 20px 0;'>
   <input type='button' value='".__("Back")."' onclick=\"location='$back_url';\">&nbsp;&nbsp;&nbsp;
   <input type='submit' name='submit_btn' value=\"".__(($_GET["type"]=="edit"? "Update" : "Add")." Withdrawal")."\" />
  </td>
 </tr>
</table>";

$content="<h2>$page_title</h2>$manage_wit";

print format_admin_page($content, $this_title, $jvscript);
?>